<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item
        label="部门负责人"
        prop="manager"
      >
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getEmployeesSimple"
          @blur="checkManager"
        >
          <el-option
            v-for="employee in employeesList"
            :key="employee.id"
            :value="employee.username"
            :label="employee.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, getDepartmentDetail, addDepartments, editDepartmentDetail } from '@/api/department'
import { getEmployeesSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkRepeatName = async(rules, value, callback) => {
      const { depts } = await getDepartments()
      let isReapt
      // console.log(depts)
      if (this.formData.id) {
        const sameDepts = depts.filter(item => item.pid === this.nodeData.pid && item.id !== this.nodeData.id)
        isReapt = sameDepts.some(item => item.name === value)
      } else {
        isReapt = depts
        const sameDepts = depts.filter(item => item.pid === this.nodeData.id)
        isReapt = sameDepts.some(item => item.name === value)
      }
      if (!isReapt) {
        callback()
      } else {
        callback(new Error('同个部门下不能同名'))
      }
    }

    const checkRepeatCode = async(rules, value, callback) => {
      const { depts } = await getDepartments()
      // console.log(this.nodeData)
      let isReapt
      if (this.formData.id) {
        isReapt = depts.some(item => item.code === value && item.id !== this.formData.id)
      } else {
        isReapt = depts.some(item => item.code === value)
      }
      if (!isReapt) {
        callback()
      } else {
        callback(new Error('编号不得重复,必为一'))
      }
    }
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '名称不能多于50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatName }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '编码不能多于50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatCode }
        ],
        introduce: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 300, message: '介绍不能多于300字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      employeesList: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeesSimple() {
      this.employeesList = await getEmployeesSimple()
      // console.log(this.employeesList)
    },
    async btnOk() {
      await this.$refs.deptForm.validate()
      if (this.formData.id) {
        await editDepartmentDetail(this.formData)
        this.$message.success('修改成功')
        this.$emit('addDeptFinish')
        this.$emit('update:showDialog', false)
      } else {
        await this.$refs.deptForm.validate()
        const data = { ...this.formData, pid: this.nodeData.id }
        await addDepartments(data)
        this.$message.success('添加成功')
        this.$emit('addDeptFinish')
        this.$emit('update:showDialog', false)
      }
    },
    btnClose() {
      this.formData = { // 直接使用赋予空值的方式进行手动清空
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.deptForm.resetFields() // 只能清理formdata里已被绑定的内容
      this.$emit('update:showDialog', false)
    },
    checkManager() {
      setTimeout(() => {
        this.$refs.deptForm.validateField('manager')
      }, 100)
    },
    async getDepartmentDetail(id) {
      this.formData = await getDepartmentDetail(id)
    }
  }
}
</script>

<style>

</style>
